<?php

namespace app\api\controller;

use app\api\ApiBaseController;
use think\admin\extend\DataExtend;
use think\facade\Db;

/**
 * 公共方法
 */
class Common extends ApiBaseController
{
    /**
     * 不需要验证登录的接口
     * @var bool
     */
    public $noNeedLoginAuth = ['area', 'allArea', 'allProvinceAndCity'];

    /**
     * 获取省市区
     */
    public function area()
    {
        $pid = $this->request->post('pid') ?? 0;
        $list = Db::name('Area')->where('pid', $pid)->order('id asc')->column('id,pid,name,ext_name');
        $this->success('ok', $list);
    }

    /**
     * 获取所有省市区
     */
    public function allArea()
    {
        $list = Db::name('Area')->order('id asc')->column('id,pid,ext_name as name');
        $list = DataExtend::arr2tree($list, 'id', 'pid','children');
        $this->success('ok', $list);
    }

    /**
     * 获取所有省市
     */
    public function allProvinceAndCity()
    {
        $list = Db::name('Area')->whereIn('deep', [0,1])->order('id asc')->column('id,pid,ext_name as name');
        $list = DataExtend::arr2tree($list, 'id', 'pid','children');
        $this->success('ok', $list);
    }
}